package com.mall.business.controller;


import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mall.common.annotation.Log;
import com.mall.common.core.controller.BaseController;
import com.mall.common.core.domain.AjaxResult;
import com.mall.common.core.page.TableDataInfo;
import com.mall.common.enums.BusinessType;
import com.mall.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mall.business.domain.TProduct;
import com.mall.business.service.ITProductService;


/**
 * 商品信息Controller
 *
 * @author ruoyi
 * @date 2025-03-29
 */
@RestController
@RequestMapping("/business/product" )
public class TProductController extends BaseController {
    @Autowired
    private ITProductService tProductService;

/**
 * 查询商品信息列表
 */
//@RequiresPermissions("business:product:list")
@GetMapping("/list" )
    public TableDataInfo list(TProduct tProduct) {
        startPage();
        List<TProduct> list = tProductService.selectTProductList(tProduct);
        return getDataTable(list);
    }

    /**
     * 导出商品信息列表
     */
    //@RequiresPermissions("business:product:export")
    @Log(title = "商品信息" , businessType = BusinessType.EXPORT)
    @PostMapping("/export" )
    public void export(HttpServletResponse response, TProduct tProduct) {
        List<TProduct> list = tProductService.selectTProductList(tProduct);
        ExcelUtil<TProduct> util = new ExcelUtil<TProduct>(TProduct. class);
        util.exportExcel(response, list, "商品信息数据" );
    }

    /**
     * 获取商品信息详细信息
     */
    //@RequiresPermissions("business:product:query")
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(tProductService.selectTProductById(id));
    }

    /**
     * 新增商品信息
     */
    //@RequiresPermissions("business:product:add")
    @Log(title = "商品信息" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TProduct tProduct) {
        return toAjax(tProductService.save(tProduct));
    }

    /**
     * 修改商品信息
     */
    //@RequiresPermissions("business:product:edit")
    @Log(title = "商品信息" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TProduct tProduct) {
        return toAjax(tProductService.updateById(tProduct));
    }

    /**
     * 删除商品信息
     */
    //@RequiresPermissions("business:product:remove")
    @Log(title = "商品信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tProductService.removeByIds(Arrays.asList(ids)));
    }

    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PostMapping("/batchOn")
    public AjaxResult batchOnShelf(@RequestBody String[] ids) {
        // 实现批量上架逻辑
        UpdateWrapper<TProduct> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids);
        updateWrapper.set("is_sj", "1");
        updateWrapper.set("sj_date",new Date());
        tProductService.update(updateWrapper);
        return  AjaxResult.success();
    }

    /**
     * 批量下架商品
     */
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PostMapping("/batchOffShelf")
    public AjaxResult batchOffShelf(@RequestBody String[] ids) {
        // 实现批量上架逻辑
        UpdateWrapper<TProduct> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids);
        updateWrapper.set("is_sj", "0");
        updateWrapper.set("sj_date",null);
        updateWrapper.set("xj_date",new Date());
        tProductService.update(updateWrapper);
        return  AjaxResult.success();
    }

}
